package com.fau.mm.action.index;

import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.mm.action.PublicAction;
import com.fau.mm.entity.MedicalAdmin;
import com.fau.mm.entity.MedicalMediaInfo;
import com.fau.mm.util.DateUtil;
import com.fau.mm.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 媒体信息管理
 * @author dangjh
 * @datetime 2014年11月6日,上午10:43:52
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class MediaAction extends PublicAction implements ModelDriven<MedicalMediaInfo> {

	private static final long serialVersionUID = -604751067777620012L;

	private MedicalMediaInfo info = new MedicalMediaInfo();
	
	/**
	 * @desc 实例化媒体信息列表页
	 * @return SUCCESS
	 * @author dangjh
	 * @datetime 2014年11月4日,下午1:22:19
	 */
	@Action(value = "init_media_info_list", results = {@Result(name = "SUCCESS", location = "init_media_info_list.jsp")})
	public String initMediaInfoList(){
		return SUCCESS;
	}
	
	/**
	 * @desc 加载实例化媒体信息数据
	 * @return null
	 * @author dangjh
	 * @datetime 2014年11月4日,下午1:24:07
	 */
	@Action(value = "media_info_veiws")
	public String mediaInfoViews(){
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		info.setEid(admin.getEnterpriseId());
		this.pageBean = this.baseAllService.getMediaService().findAllMediaInfo(map, info);
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		HttpServletResponse response = ServletActionContext.getResponse();
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 实例化媒体添加界面
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月4日,下午1:40:19
	 */
	@Action(value = "medical_media_add", results = {@Result(name = "SUCCESS", location = "medical_media_add.jsp")})
	public String mediaAdd(){
		return SUCCESS;
	}
	
	/**
	 * @desc 保存添加的媒体信息
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月4日,下午1:41:42
	 */
	@Action(value = "medical_media_save")
	public String meidaSave(){
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		map = new HashMap<String, Object>();
		boolean flag = this.baseAllService.getMediaService().save(info, admin);
		if(flag){
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 实例化媒体信息编辑界面
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月4日,下午1:45:33
	 */
	@Action(value = "medical_media_edit", results = {@Result(name = "SUCCESS", location = "medical_media_edit.jsp")})
	public String mediaEdit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 保存编辑的媒体信息
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月4日,下午1:46:47
	 */
	@Action(value = "media_edit_save")
	public String mediaEditSave(){
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(info.getId())){
			boolean flag = this.baseAllService.getMediaService().saveEdit(info, admin);
			if(flag){
				map.put("success", true);
			}else{
				map.put("success", false);
			}
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 删除媒体信息
	 * @return null
	 * @author dangjh
	 * @datetime 2014年11月4日,下午1:49:45
	 */
	@Action(value = "medical_media_delete")
	public String mediaDelete(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(info.getId())){
			boolean flag = this.baseAllService.getMediaService().delete(info);
			if(flag) {
				map.put("success", "true");
			} else {
				map.put("success", "false");
			}
		}else{
			map.put("success", "error");
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 查看媒体信息
	 * @return SUCCESS
	 * @author dangjh
	 * @datetime 2014年11月6日,下午5:05:15
	 */
	@Action(value = "medical_media_view", results = {@Result(name = "SUCCESS", location = "medical_media_view.jsp")})
	public String mediaView(){
		return SUCCESS;
	}
	
	/**
	 * @desc 导出当前页面数据
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月20日,上午11:51:07
	 */
	@Action(value = "export_current_media_info_data")
	public String exportCurrent(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		String ids = request.getParameter("ids");
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"媒体信息管理"+DateUtil.getDate()+".xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getMediaService().currentPage(ids);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 导出所有媒体信息
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月20日,上午11:51:49
	 */
	@Action(value = "export_all_media_info_data")
	public String exportAll(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"媒体信息管理"+DateUtil.getDate()+".xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getMediaService().allPage(user);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 查询地址省
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月8日,下午6:04:00
	 */
	@Action(value = "medical_media_find_province")
	public String findProvinceList() {
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getMediaService().findProvinceList();
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 根据省份id 查询地址城市集合
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月8日,下午6:05:56
	 */
	@Action(value = "medical_media_find_city")
	public String findCityList() {
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getMediaService().findCityListByPid(info.getProvinceId());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 根据城市id 查询地区集合
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月8日,下午6:08:10
	 */
	@Action(value = "medical_media_find_district")
	public String findDistrictList() {
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getMediaService().findDistrictListByCid(info.getCityId());
		formatJSON(response, list);
		return null;
	}
	
	@Override
	public MedicalMediaInfo getModel() {
		return info;
	}

}
